package com.wnhz.work.yz.fragment.order;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AllOrderActivity;
import com.wnhz.work.yz.activity.ChoosePayActivity;
import com.wnhz.work.yz.activity.OrderDetailsActivity;
import com.wnhz.work.yz.activity.ShopProgressActivity;
import com.wnhz.work.yz.adapter.OrderAdapter;
import com.wnhz.work.yz.bean.OrderBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ---全部
 */
public class OrderFragment1 extends Fragment implements View.OnClickListener {
    private View view;
    private ListView myListView;
    private OrderAdapter mAdapter;
    private AllOrderActivity activity;
    private List<OrderBean> beanList = new ArrayList<>();
    private static final String TAG = "OrderFragment1";
    private int indexPosition, indexPosition2, indexPosition3, index4;

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_order_fragment1, container, false);
        activity = (AllOrderActivity) getActivity();
        upload("0");
        initView();
        return view;
    }

    public void upload(final String type) {
        activity.showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        XUtil.Post(Url.ORDER_GETORDERLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("===OrderFragment1 订单===", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    beanList.clear();
                    if (jsonArray != null) {
                        beanList.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            OrderBean bean = new OrderBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), OrderBean.class);
                            beanList.add(bean);
                        }
                        Log.e(TAG, "initView:==1 " + beanList.size());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                activity.closeDialog();
                super.onError(ex, isOnCallback);
                Log.e(TAG, "onError: " + ex);
            }
        });
    }

    private void initView() {
        myListView = (ListView) view.findViewById(R.id.myListView);
        mAdapter = new OrderAdapter(getActivity(), beanList, this);
        myListView.setAdapter(mAdapter);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        int index;
        switch (v.getId()) {
            case R.id.ll_top1:
                index = (int) v.getTag(R.id.ll_top1);
                intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra("orderId", beanList.get(index).getOrder_id());
                startActivity(intent);
                break;
            //去支付
            case R.id.tv_go_pay:
                index = (int) v.getTag(R.id.tv_go_pay);
                intent = new Intent(getActivity(), ChoosePayActivity.class);
                intent.putExtra("order_no", beanList.get(index).getOrder_no());
                startActivity(intent);
                break;

            //取消订单
            case R.id.tv_cancel:
                index = (int) v.getTag(R.id.tv_cancel);
                indexPosition = index;
                if ("1".equals(beanList.get(index).getOrder_status())) {
                    activity.showDialogv7("确定取消该订单？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition).getOrder_id();
                            upcancel(orderId);
                        }
                    });
                } else {
                    activity.MyToast("该订单不可取消");
                }
                break;

            //删除订单
            case R.id.tv_delete:
                index = (int) v.getTag(R.id.tv_delete);
                indexPosition2 = index;
                if ("0".equals(beanList.get(index).getOrder_status()) || "4".equals(beanList.get(index).getOrder_status()) || "5".equals(beanList.get(index).getOrder_status())) {
                    activity.showDialogv7("确定删除该订单？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition2).getOrder_id();
                            updelete(orderId);
                        }
                    });

                } else {
                    activity.MyToast("该订单不可删除");
                }
                break;

            //订单详情
            case R.id.ll_top:
                index = (int) v.getTag(R.id.ll_top);
                intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra("orderId", beanList.get(index).getOrder_id());
                startActivity(intent);
                break;

            //取消退款
            case R.id.tv_no_refund:
                index = (int) v.getTag(R.id.tv_no_refund);
                indexPosition3 = index;
                if ("6".equals(beanList.get(index).getOrder_status())) {
                    activity.showDialogv7("确定取消退款？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String orderId = beanList.get(indexPosition3).getOrder_id();
                            uprefund(orderId);
                        }
                    });
                } else {
                    activity.MyToast("该订单不可取消");
                }
                break;
            //取消退款
            case R.id.buy_two:
                index = (int) v.getTag(R.id.buy_two);

                if ("0".equals(beanList.get(index).getOrder_status()) || "4".equals(beanList.get(index).getOrder_status()) || "5".equals(beanList.get(index).getOrder_status())) {
                    String orderId = beanList.get(index).getOrder_id();
                    buyTwo(orderId);
                } else {
                    activity.MyToast("商品已过期");
                }
                break;
            //提醒发货
            case R.id.shipments:
                index = (int) v.getTag(R.id.shipments);
                if ("2".equals(beanList.get(index).getOrder_status())) {
                    String orderId = beanList.get(index).getOrder_id();
                    upLoadShipments(orderId);
                }
                break;
            //进度
            case R.id.shop_progress:
                index = (int) v.getTag(R.id.shop_progress);
                if ("2".equals(beanList.get(index).getOrder_status()) || "3".equals(beanList.get(index).getOrder_status())) {
                    intent = new Intent(getContext(), ShopProgressActivity.class);
                    String orderId = beanList.get(index).getOrder_id();
                    intent.putExtra("orderId", orderId);
                    startActivity(intent);
                    //shopProgress(orderId);
                }
                break;
            //申请退款
            case R.id.tv_refund1:
                index = (int) v.getTag(R.id.tv_refund1);
                index4 = index;
                activity.showDialogv7("确认申请退款？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if ("2".equals(beanList.get(index4).getOrder_status()) || "3".equals(beanList.get(index4).getOrder_status())) {
                            String orderId = beanList.get(index4).getOrder_id();
                            uprefund2(orderId);
                        }
                    }
                });

                break;
            default:
                break;
        }
    }


    private void upLoadShipments(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_REMINDDELIVERY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:REFUNDORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        upload(type);
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void buyTwo(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_AGAINBUY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:CANCELREFUND== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        if (mAdapter != null) {
                            mAdapter.notifyDataSetChanged();
                        }
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void uprefund2(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_REFUNDORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:REFUNDORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        upload("0");
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void uprefund(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_CANCELREFUND, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:CANCELREFUND== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        upload("0");
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });

    }

    private void updelete(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_DELORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:ORDER_DELORDER== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        upload("0");
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void upcancel(String orderId) {
        activity.showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_CANCELORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                activity.closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: orderId=== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        activity.MyToast(info);
                        upload("0");
                    } else {
                        activity.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                activity.closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }
}
